using System;
using System.IO;
namespace Slaks.Web.Parser.Utils
{
	/// <summary>
	/// Summary description for LocalPageDownload.
	/// </summary>
	public class LocalPageDownload
	{
		private static bool m_forceEncodingDetection = false;
    /// <summary>
    /// Read the HTML file
    /// </summary>
    public static string GetWebPage(string filepage,System.Text.Encoding encoding)
    {
			StreamReader reader = null;
      try
      {
				reader = new StreamReader(filepage,encoding);
        string buffer = reader.ReadToEnd();
				if (LocalPageDownload.IsEncodingDetectionForced == true)
				{
					string enc = WebPageDownload.EstimateEncodingSecondChance(buffer);
					if (enc != String.Empty)
					{
						try
						{
							encoding = System.Text.Encoding.GetEncoding(enc);
							reader.Close();

							reader = new StreamReader(filepage,encoding);
							buffer = reader.ReadToEnd();
						}
						catch(Exception)
						{
							
						}
					}

				}
        return buffer;
      }
      catch(Exception ex) { throw ex; }
      finally
      {
        if (reader != null) reader.Close();
      }
    }

		/// <summary>
		/// Force encoding detection of the web page stored locally
		/// </summary>
		public static bool IsEncodingDetectionForced
		{
			get { return m_forceEncodingDetection ; }
			set { m_forceEncodingDetection = value; }
		}
	}
}
